package com.caigou.modules.forum.widget

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Window
import android.widget.Button
import android.widget.CheckBox
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.forum.R
import com.caigou.modules.forum.bean.FifaItemBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * @author #Suyghur.
 * Created on 2021/12/25
 */
class FifaTypeDialog(context: Context, private val fifaTypes: MutableList<FifaItemBean>) : Dialog(context) {

    lateinit var mAdapter: FifaTypeItemAdapter
    private lateinit var recyclerView: RecyclerView
    lateinit var btnConfirm: Button

    init {
        setCanceledOnTouchOutside(true)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        initView()
    }

    private fun initView() {
        setContentView(R.layout.dialog_fifa_type)

        recyclerView = findViewById(R.id.fifa_type_dialog_recycler_view)
        mAdapter = FifaTypeItemAdapter(fifaTypes)
        recyclerView.run {
            layoutManager = GridLayoutManager(context, 3)
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(context))
        }

        btnConfirm = findViewById(R.id.fifa_type_dialog_btn_confirm)

    }

    class FifaTypeItemAdapter(dataset: MutableList<FifaItemBean>) :
        BaseQuickAdapter<FifaItemBean, BaseViewHolder>(R.layout.item_fifa_type_list, dataset) {

        private val checkBoxes = mutableListOf<CheckBox>()
        var checkPos = -1

        override fun convert(holder: BaseViewHolder, item: FifaItemBean) {
            with(holder.getView<CheckBox>(R.id.item_fifa_type_cb)) {
                text = item.fifaTypeName
                val pos = getItemPosition(item)
                checkBoxes.add(this)
                setOnClickListener {
                    if (checkPos != -1) {
                        if (checkPos != pos) {
                            checkBoxes[checkPos].isChecked = false
                            isChecked = true
                            checkPos = pos
                        }
                    } else {
                        isChecked = true
                        checkPos = pos
                    }
                }
            }
        }
    }

}